package com.company.OfferGuide;

import java.util.Stack;

public class 栈的压入和弹出序列 {

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if (popped.length == 0 && pushed.length == 0) return true;
        else if (popped.length == 0 || pushed.length == 0) return false;

        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushed.length; i++) {
            stack.push(pushed[i]);
            while ( !stack.isEmpty() && j < popped.length && stack.peek() == popped[j]) {
                stack.pop();
                j++;
            }
        }

        stack.empty();
        return stack.isEmpty();
    }

}
